08. Exercise: Add View Interactivity
21 6 AAK Custom Views Interactivity Slide-SC V2
Android Developer Documentation
Reflect
QUESTION:
Why do you override performClick()? What happens if you override onClickListener() instead?
ANSWER:
The performClick() method calls onClickListener(). If you override performClick(), another contributor can still override onClickListener(). For example, if you create a custom view and make it available through a library for use or subclassing, its user can add their own click handling through onClickListener().
Exercise
In this exercise you are going to add interactivity to the view.
- In
DialView.kt, inside theFanSpeedenumeration, add an extension functionnext()that changes the current fan speed to the next speed in the list (fromOFFtoLOW,MEDIUM, andHIGH, and then back toOFF). The complete enumeration now looks like this:
private enum class FanSpeed(val label: Int) {
OFF(R.string.fan_off),
LOW(R.string.fan_low),
MEDIUM(R.string.fan_medium),
HIGH(R.string.fan_high);
fun next() = when (this) {
OFF -> LOW
LOW -> MEDIUM
MEDIUM -> HIGH
HIGH -> OFF
}
}
- Inside the
DialViewclass, just before theonSizeChanged()method, add aninit()block. Setting the view'sisClickableproperty totrueenables that view to accept user input.
init {
isClickable = true
}
- Below
init(), override theperformClick()method with this code:
override fun performClick(): Boolean {
if (super.performClick()) return true
fanSpeed = fanSpeed.next()
contentDescription = resources.getString(fanSpeed.label)
invalidate()
return true
}
The call to
super.performClick()must happen first, which enables accessibility events as well as callsonClickListener().The next two lines increment the speed of the fan with the
next()method, and set the view's content description to the string resource representing the current speed (off, 1, 2 or 3).Finally, the invalidate() method invalidates the entire view, forcing a call to
onDraw()to redraw the view. If something in your custom view changes for any reason, including user interaction, and the change needs to be displayed, callinvalidate().
- Run the app.
- Tap the
DialViewelement to move the indicator from off to 1. The dial should turn green. - With each tap, the indicator should move to the next position.
- When the indicator returns to off, the dial should turn gray again.